<%@ page language="java"  pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
      	<style type="text/css">
		* {
			font-size: 12px;
		}
    	</style>
		<link rel="stylesheet" type="text/css" href="<%=basePath%>js/jquery/themes/default/easyui.css">
		<link rel="stylesheet" type="text/css" href="<%=basePath%>js/jquery/themes/icon.css">
		<script type="text/javascript" src="<%=basePath%>js/jquery/jquery-1.7.2.min.js"></script>
		<script type="text/javascript" src="<%=basePath%>js/jquery/jquery.easyui.min.js"></script>
		<script type="text/javascript" src="<%=basePath%>js/jquery/locale/easyui-lang-zh_CN.js"></script>
 		<script type="text/javascript" src="<%=basePath%>js/ztree/js/jquery.ztree.core-3.5.js"></script>
 		<script type="text/javascript" src="<%=basePath%>js/ztree/js/jquery.ztree.excheck-3.5.js"></script>
 	    <link rel="stylesheet" href="<%=basePath%>js/ztree/css/zTreeStyle/zTreeStyle.css" type="text/css">
 		<script type="text/javascript">
 		var pid = -10 ;
 		var pname = "" ;
 		var vailflag=false;
 		var editflag = false;
 		function oncheckClick()
 		{
 			var treeObj = $.fn.zTree.getZTreeObj("treeDemo");
			var nodes = treeObj.getCheckedNodes(true);
			var id = nodes[0].id ;
			pid = id ;
			pname = nodes[0].name ;
			var queryParams = $('#dataGrid').datagrid('options').queryParams;  
			queryParams.pid = id;
			$('#dataGrid').datagrid({
				queryParams:queryParams,
				url :"<%=basePath %>dept/getDeptListByPId.xhtml"
			});
 		}
		var setting = {
			async: {
				enable: true,
				url: "<%=basePath %>dept/getAllDept.xhtml",
				autoParam: ["id"]
			},
			check: {
				enable: true,
				chkStyle: "radio",
				radioType: "all"
			},
			callback:{
				onCheck :oncheckClick
			},
			data: {
				simpleData: {
					idKey: "id",
					pIdKey:"pid",
					enable: true
				}
			}
		};

		
		
		var code;		
		function setCheck() {
			var type = $("#level").attr("checked")? "level":"all";
			setting.check.radioType = type;
			showCode('setting.check.radioType = "' + type + '";');
			$.fn.zTree.init($("#treeDemo"), setting, null);
		}
		function showCode(str) {
			if (!code) code = $("#code");
			code.empty();
			code.append("<li>"+str+"</li>");
		}
		
		$(document).ready(function(){
			setCheck();			
		});
 		</script>
 				<script type="text/javascript">
 			function sub()
		{
						if( $('#ff').form('validate'))
		{
			$('#ff').submit();
			vailflag = false ;
		}
		}
		function closeW()
		{
			$('#logicWindow').window('close');
		}
		
		function showEdit()
		{
			editflag = true;
			$('#deptSpan').html('');
			var rows = $('#dataGrid').datagrid('getSelections');
			if(rows.length != 1)
			{
				$.messager.alert('提示','请选中一条记录');
			}else {
				$('#ff').form({
				onSubmit:function (){
					var t =  $(this).form('validate');
					if(t)
					{
						$('#btnsub').linkbutton({disabled:true});
					}
					return t;
				},
				success:function(data){
					var json = eval("("+data+")");
					if(!json.state)
					{
						alert(json.msg);
					}
					$('#btnsub').linkbutton({disabled:false});
					closeW();
					$("#dataGrid").datagrid("reload");
				}
			});
				var now=new Date();
				var number = now.getYear().toString()+now.getMonth().toString()+now.getDate().toString()+now.getHours().toString()+now.getMinutes().toString()+now.getSeconds().toString();
		
				$('#ff').form('load','<%=basePath%>sys/dept/deptAction!load.action?deptEntity.id='+rows[0].id+'&t='+number);
				$('#logicWindow').window('open');
			}
			document.getElementById('ff').action = '<%=basePath%>/sys/dept/deptAction!update.action' ;
		}
		
		
		
		
		function deleteLogic()
		{
			var rows = $('#dataGrid').datagrid('getSelections');
			if(rows.length >0 )
			{
				var treeObj = $.fn.zTree.getZTreeObj("treeDemo");
				
				var ids = []; 
				for (var i = 0 ; i < rows.length ; i ++)
				{
				
					var nodes = treeObj.getNodesByParam("pid", rows[i].id, null);
					if(nodes.length != 0)
					{
						alert('选中的部门还存在子部门,不允许删除');
						return ;
					}
					ids.push(rows[i].id);
				}
				var now=new Date();
				var number = now.getYear().toString()+now.getMonth().toString()+now.getDate().toString()+now.getHours().toString()+now.getMinutes().toString()+now.getSeconds().toString();
		
				$.ajax({
					url:"<%=basePath%>sys/dept/deptAction!delete.action?deleteid="+ids.join(",")+"&rand="+Math.random(),
					success:function (data){
					var json = eval("("+data+")");
						if(!json.state)
						{
							alert(json.msg);
						}else {
							$("#dataGrid").datagrid("reload");
						}
					}
				});
			}else {
				$.messager.alert('提示','请选择需要删除的数据');
			}
		
		}
 			$(document).ready(function (){
			$('#dataGrid').datagrid({
				width:"auto",
				pageSize:20,
				title:"部门管理",
				iconCls:'icon-save',
				pagination:true,
				striped:true,
				loadMsg:"加载中...",
				rownumbers:true,
				collapsible:true,
				fit:true,
				frozenColumns:[[ 
                {field:'id',checkbox:true}
				]], 
				toolbar:[{
					id:"btnadd",
					text:"新增部门",
					iconCls: "icon-add",
					handler:function ()
					{
						if(pid == -10)
						{
							alert('请选择部门树');
							return ;
						}else {
						document.getElementById("lastProduct").innerHTML = pname;
						
						addW();
						}
						
					}
				},{
					id:"btnedit",
					text:"修改部门信息",
					iconCls: "icon-edit",
					handler:function()
					{
						showEdit();
					}
				},{
					id:"btndelete",
					text:"删除部门",
					iconCls: "icon-remove",
					handler:function()
					{
						deleteLogic();
					}
				}],
				columns:[[
					{field:"name",title:"部门名称",width:"150"},
					{field:"createtime",title:"创建时间",width:"200"}
				]]
			});	
			});	
			
			function addW()
			{
			editflag = true;
					$('#deptSpan').html('');
					$('#ff').form('clear');
					document.getElementById("pid").value = pid;
					$('#ff').form({
						onSubmit:function (){
							var t =  $(this).form('validate');
							if(t)
							{
								$('#btnsub').linkbutton({disabled:true});
							}
							return t;
						},
						success:function(data){
							var json = eval("("+data+")");
							if(!json.state)
							{
								alert(json.msg);
							}else {
								setCheck();
							}
							$('#btnsub').linkbutton({disabled:false});
							closeW();
							$("#dataGrid").datagrid("reload");
						}
					});
					document.getElementById('ff').action = '<%=basePath%>dept/addDept.xhtml' ;
					$('#logicWindow').window('open');
			}
			
			function searchForm(){
				var searchdeptname = document.getElementById("searchname").value;
				var queryParams=$('#dataGrid').datagrid('options').queryParams;
				queryParams.name = searchdeptname;
				$('#dataGrid').datagrid({
					queryParams:queryParams
				});
				$('#dataGrid').datagrid('reload');
			}
			
			
			 function clearForm(){
			 	document.getElementById("searchname").value="";
			 }
			
			function validAjaxDeptname(obj)
			{	
				if(!obj.value || obj.value == '' )
				{
					
				}else if(editflag){
					vailflag=true;	
				}else {
				$.ajax({
					type:"post",
					data:{"searchdeptname":obj.value},
					url:"<%=basePath %>sys/dept/deptAction!validDeptname.action",
					success:function (data){
						if(data > 0)
						{
							$('#deptSpan').html('<font color="red">已存在的部门名</font>');
							document.getElementById("username").focus();
							vailflag=false;
						}else if(data == 0){
							$('#deptSpan').html('');
							vailflag=true;							
						}
					}
				});
				}
				
			}			
 		</script>
  </head>
  
  <body class="easyui-layout" >
  		<div region="west" split="true" title="部门分类" style="width: 200px; padding: 1px;">
			<ul id="treeDemo" class="ztree"></ul>
		</div>

		<div region="center" split="true" title="部门详细信息">
			<div  class="easyui-layout" fit="true">
				<div region="center">
					<table id="dataGrid"></table>
				</div>
				<div region="north" style="height: 45px;">
					<form action="" id="searchForm">
						<table>
							<tr>
								<td>
									部门名称:
								</td>
								<td>
									<input type="text" id="searchname" name="searchdeptname"/>
								</td>
								<td>
									<a class="easyui-linkbutton" iconCls="icon-search"
										onclick="searchForm()">搜索</a>
								</td>
								<td>
									<a class="easyui-linkbutton" iconCls="icon-reload"
										onclick="clearForm()">清空</a>
								</td>
							</tr>
						</table>
					</form>
				</div>
			</div>
		</div>
		<div id="logicWindow" class="easyui-window" title="角色操作"
			iconCls="icon-save"
			style="width: 500px; height: 200px; padding: 5px;" closed = "true"  >
			<div class="easyui-layout" fit="true">
				<div region="center" border="false"
					style="background: #fff; border: 1px solid #ccc;">
					<form id="ff" method="post">
						
						<input type="hidden" name="pid" id="pid">
						<input type="hidden" name="id" id="id">
						<table>
							<tr>
									<td>
										<label for="txt">
											上级部门类型:
										</label>
									</td>
									<td>
										<span id = "lastProduct" ></span>
									</td>
								</tr>
							<tr>
								<td>
									<label for="classname">
										部门名称:
									</label>
								</td>
								<td>
									<input class="easyui-validatebox" type="text"
										style="width: 200" id="deptname" name="name"
										onblur="validAjaxDeptname(this)" validType="length[0,50]"
										required="true"></input>
										<span id="deptSpan"></span>
								</td>
							</tr>
							
						</table>
					</form>
				</div>
				<div region="south" border="false"
					style="text-align: right; padding: 5px 0;">
					<a class="easyui-linkbutton" id="btnsub" iconCls="icon-ok"
						onclick = "sub()">提 交</a>
					<a class="easyui-linkbutton" iconCls="icon-cancel"
						onclick = "closeW()">关闭</a>
				</div>
  </body>
</html>
